package com.czx.reflection.controller;

import com.czx.reflection.pojo.User;
import sun.reflect.generics.tree.ReturnType;

import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
//获取泛型的信息
public class test04 {
    public void test01(Map<String, User> map, List<User> list){
        System.out.println("test01");
    }
    public Map<String,User> test02(){
        System.out.println("test02");
        return null;
    }

    public static void main(String[] args) throws NoSuchMethodException {
        //获取参数为泛型的信息
        Method method = test04.class.getMethod("test01", Map.class, List.class);
        Type[] genericParameterTypes = method.getGenericParameterTypes();
        for (Type type : genericParameterTypes) {
            System.out.println("#"+type);
            if(type instanceof ParameterizedType){//判断type属于参数化类型（ParameterizedType）
                Type[] actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments();//获得真实类型
                for (Type actualTypeArgument : actualTypeArguments) {
                    System.out.println(actualTypeArgument);
                }
            }
        }
        System.out.println("++++++++++++++++++++++++++++++++++++++++");
        //获取返回值为泛型的信息
        Method test02 = test04.class.getMethod("test02");
        Type genericReturnType = test02.getGenericReturnType();
        System.out.println(genericReturnType);
        if(genericReturnType instanceof ParameterizedType){//判断type属于参数化类型（ParameterizedType）
            Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();//获得真实类型
            for (Type actualTypeArgument : actualTypeArguments) {
                System.out.println(actualTypeArgument);
            }
        }
    }
}
